package com.syj.collect.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.syj.collect.app.R;
import com.syj.collect.entry.AllFragmentBean;
import com.syj.collect.http.ApiServise;
import com.syj.collect.listnner.ItemListeners;
import com.syj.collect.model.ProjectDetailActivity;
import com.syj.collect.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * created by XXX on 2021/3/4.
 */
public class AdapterProjectAllFragment extends RecyclerView.Adapter<AdapterProjectAllFragment.AdapterProjectHolder> {
    private int type = 0;
    private List<AllFragmentBean.DataBean.ItemBean> list = new ArrayList<>();
    private ItemListeners listnner;


    public AdapterProjectAllFragment(int i) {
        type = i;
        list = new ArrayList();
    }


    @NonNull
    @Override
    public AdapterProjectHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(0 == type ? R.layout.project_list_item1 : R.layout.project_list_item2, parent, false);
        return new AdapterProjectHolder(view);
    }

    public void setclickListnner(ItemListeners listener) {
        this.listnner = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull AdapterProjectHolder holder, int position) {
//        holder.img.setImageResource(R.drawable.my_head);

        GlideUtils.PicImage(holder.img, ApiServise.BASEURLFILE+list.get(position).getUrls().get(0));
        holder.content1.setText(list.get(position).getName());
        holder.content2.setText(list.get(position).getPollutionStatus() + "");
        holder.content3.setText(list.get(position).getAddMsg()+ "");
        holder.collect_state.setImageResource(list.get(position).getIsCollect() == 1 ? R.drawable.collect_true : R.drawable.collect_false);

        // holder.collect.setText(iscollect?"收藏":"收藏");
        holder.collect_state.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //holder.collect.setText(iscollect?"取消收藏":"收藏");
                ;
                boolean iscollect = list.get(position).getIsCollect() == 1;

                // holder.collect_state.setImageResource(iscollect?R.drawable.collect_true:R.drawable.collect_false);
                listnner.onmListenner(list.get(position), position, !iscollect);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProjectDetailActivity.StartAction(holder.itemView.getContext(), list.get(position).getId() + "");

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public void upData(List<AllFragmentBean.DataBean.ItemBean> taskListBeanData) {

        if (taskListBeanData.isEmpty()) {
        } else {

            list.addAll(taskListBeanData);
        }
        notifyDataSetChanged();

    }

    public void upData(AllFragmentBean.DataBean.ItemBean bean) {
        if (list.contains(bean)) {
            int pos=list.indexOf(bean);
            boolean iscollect = list.get(pos).getIsCollect() == 1;
            list.get(pos).setIsCollect(iscollect ? 0 : 1);
            notifyItemChanged(pos);
        }
    }
    public void ClearData() {
       list.clear();
       notifyDataSetChanged();
    }

    public void updataType(int i) {
        type = i;
        notifyDataSetChanged();
    }

    public static class AdapterProjectHolder extends RecyclerView.ViewHolder {

        private final TextView content1;
        private final TextView content2, collect;
        private final TextView content3;
        private final ImageView img;
        private final ImageView collect_state;

        public AdapterProjectHolder(@NonNull View itemView) {
            super(itemView);

            content1 = (TextView) itemView.findViewById(R.id.adapter_pro_item_content1);
            content2 = (TextView) itemView.findViewById(R.id.adapter_pro_item_content2);
            content3 = (TextView) itemView.findViewById(R.id.adapter_pro_item_content3);
            collect = (TextView) itemView.findViewById(R.id.adapter_pro_item_collect);
            collect.setVisibility(View.GONE);
            collect_state = (ImageView) itemView.findViewById(R.id.adapter_pro_item_collect_state);
            img = (ImageView) itemView.findViewById(R.id.adapter_pro_item_img);
        }
    }
}
